﻿namespace TimeTravel.XnaLibrary.GamePlay
{
	/// <summary>
	/// Represents a player commanded by artificial intelligence in the TimeTravel game.
	/// </summary>
	public class AI : Player
	{
		private PriorityOrder[] mOrders;
		private int mTimeMana;

		/// <summary>
		/// Gets or sets the starting time mana.
		/// </summary>
		public static int StartingTimeMana = 300;
		/// <summary>
		/// Gets or sets the mana cost for the least expensive time travel.
		/// </summary>
		public static int MinimumTimeTravelCost = 100;

		/// <summary>
		/// Gets an array of PriorityOrders defining the AI decisions.
		/// </summary>
		public PriorityOrder[] Orders
		{
			get { return mOrders; }
		}

		/// <summary>
		/// Gets or sets the current amount of time mana.
		/// </summary>
		public int TimeMana
		{
			get { return mTimeMana; }
			set { mTimeMana = value; }
		}

		/// <summary>
		/// Initializes a new AI instance with the specified race, starting position and orders.
		/// </summary>
		/// <param name="race">The race of the AI.</param>
		/// <param name="unitStartingPosition">The starting position for each unit.</param>
		/// <param name="orders">Array of PriorityOrders defining the AI decisions.</param>
		public AI(Race race, int unitStartingPosition, PriorityOrder[] orders) :
			base(race, unitStartingPosition)
		{
			mOrders = orders;
			mTimeMana = StartingTimeMana;
		}

		/// <summary>
		/// Initializes a new AI instance by copying the specified AI state.
		/// </summary>
		/// <param name="ai">The AI object to copy from.</param>
		public AI(AI ai) : 
			base(ai)
		{
			this.mTimeMana = ai.mTimeMana;
			this.mOrders = new PriorityOrder[ai.Orders.Length];
			for (int i = 0; i < mOrders.Length; i++)
			{
				PriorityOrder currentOrder = ai.Orders[i];
				this.mOrders[i] = new PriorityOrder(currentOrder.Priority, currentOrder.Condition, currentOrder.Command);
			}
		}

		/// <summary>
		/// Tries to execute all commands the AI has received.
		/// </summary>
		public void ExecuteOrders()
		{
			for (int i = 0; i < mOrders.Length; i++)
			{
				if (mOrders[i].Condition(this))
				{
					mOrders[i].Command(this);
				}
			}
		}
	}
}
